//
//  PersonalLabelViewController.m
//  SouthChina
//
//  Created by kc on 15/7/7.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "PersonalLabelViewController.h"
#import "DBSphereView.h"
#import "AddPersonalLabelViewController.h"

@interface PersonalLabelViewController ()
@property (strong, nonatomic)  UIImageView *userimage;
@property (nonatomic, retain) DBSphereView *sphereView;
@property (strong, nonatomic) UIButton * addLabelBtn;//添加按钮

@end

@implementation PersonalLabelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTitleBarWithBackBtn:@"标签" needReturn:YES];
   
    _sphereView = [[DBSphereView alloc] initWithFrame:CGRectMake(0, TITLE_BAR_HEGIHT+(YNEW)+50, SCREEN_WIDTH-100, SCREEN_HEIGHT-TABBARHEIGHT-50)];
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
     NSString *strusrname = [DE getGlobalVar:LOGIN_USER_TAGS] == nil?@"":[DE getGlobalVar:LOGIN_USER_TAGS];
    if(![strusrname isEqualToString:@""])
    {
        NSArray *list=[strusrname componentsSeparatedByString:@"，"];
        NSLog(@"标签初始化大小：%lu",[list count]);
        for(NSInteger i = 0; i < [list count]; i ++)
        {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            [btn setTitle:[list objectAtIndex:i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:24.];
            btn.frame = CGRectMake(0, 0, 60, 60);
            btn.backgroundColor = [UIColor colorWithRed:arc4random_uniform(100)/100. green:arc4random_uniform(100)/100. blue:arc4random_uniform(100)/100. alpha:1];
            btn.layer.masksToBounds=YES;
            btn.layer.cornerRadius=btn.frame.size.height/2;
            btn.titleLabel.lineBreakMode = 0;
            
            [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
            [btn addTarget:self action:@selector(buttonPressed2:) forControlEvents:UIControlEventTouchUpInside];
            
            [array addObject:btn];
            [_sphereView addSubview:btn];
        }
    }
    
    [_sphereView setCloudTags:array];
    _sphereView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_sphereView];
    
    //用户头像
    NSString *headStr = [DE getGlobalVar:LOGIN_USER_HEADIMG] == nil?@"":[DE getGlobalVar:LOGIN_USER_HEADIMG];
    NSURL *url= [NSURL URLWithString: [headStr hasPrefix:@"http"]?headStr:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,headStr]];
    
    _userimage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2-180, 50, 50)];
    [_userimage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    _userimage.layer.masksToBounds = YES;
    _userimage.layer.cornerRadius = _userimage.frame.size.height/2;
    _userimage.contentMode =  UIViewContentModeCenter;
    [_sphereView addSubview:_userimage];
    
    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"label_bg.jpg"]];
   //_sphereView backgroundColor = bgColor;
    self.view.backgroundColor= bgColor;
    
    
    //标题栏增加+按钮
    UIImage *img=[UIImage imageNamed:@"add_more.png"];
    _addLabelBtn = [[UIButton alloc] initWithFrame:CGRectMake(255, self.titlebar.frame.origin.y+30, 20, 20)];
    [_addLabelBtn addTarget:self action:@selector(addLableBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_addLabelBtn setTitle:@"" forState:UIControlStateNormal];
    [_addLabelBtn setBackgroundImage:img forState:UIControlStateNormal];
    [self.titlebar addSubview:_addLabelBtn];
    
}

- (void)buttonPressed:(UIButton *)btn
{
    [_sphereView timerStop];
    
    [UIView animateWithDuration:0.3 animations:^{
        btn.transform = CGAffineTransformMakeScale(2., 2.);
    } completion:^(BOOL finished) {
    }];
}

- (void)buttonPressed2:(UIButton *)btn
{
    [_sphereView timerStop];
    
    [UIView animateWithDuration:0.3 animations:^{
        btn.transform = CGAffineTransformMakeScale(1., 1.);
    } completion:^(BOOL finished) {
        [_sphereView timerStart];
    }];
}

-(void)addLableBtn:(id)sender
{
    AddPersonalLabelViewController * addPersonalLabelView = [[AddPersonalLabelViewController alloc] init];
    [kAppDelegate.iCSDrawerController.navigationController pushViewController:addPersonalLabelView animated:NO ];
    
}

/*
- (void)buttonPressed:(UIButton *)btn
{
    [_sphereView timerStop];
    
    [UIView animateWithDuration:0.3 animations:^{
        btn.transform = CGAffineTransformMakeScale(2., 2.);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            btn.transform = CGAffineTransformMakeScale(1., 1.);
        } completion:^(BOOL finished) {
            [_sphereView timerStart];
        }];
    }];
}*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
